package com.example.ecommercemanagesystem.controller;


import com.example.ecommercemanagesystem.entity.User;
import com.example.ecommercemanagesystem.service.UserService;
import com.example.ecommercemanagesystem.util.IsFish;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/mytest")

    public String mytest(HttpServletRequest request) {

        List<User> list = userService.getAllUser();

        request.setAttribute("list", list);

        return "index";

    }

    //管理员登录
    @RequestMapping("/checkLogin")
    public void checkLogin(String user_name, String user_pwd, HttpServletResponse response, HttpServletRequest request)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        IsFish isFish=new IsFish();
        if(isFish.isFish(request)){
            User user=userService.getUser(user_name,user_pwd);

            if(user!=null&&"admin".equals(user_name)&&"123".equals(user_pwd)){
                request.getSession().setAttribute("loginuser",user);
                out.print("<script>alert('登录成功');location.href='index'</script>");

            }else{
                out.print("<script>alert('登录失败,您的权限不够，请联系管理员');history.go(-1);</script>");

            }
            out.flush();
            out.close();
        }else{
            out.print("<script>alert('滚吧，盗链狗！！！！');history.go(-1);</script>");
        }
        out.flush();
        out.close();


    }
    //注销
    @RequestMapping("/loginout")
    public  void loginout(HttpServletResponse response,HttpServletRequest request)throws Exception{
        HttpSession session=request.getSession();
        User user= (User) request.getSession().getAttribute("loginuser");
        session.invalidate();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.print("<script>alert('会话已失效，请重新登录');location.href='login'</script>");

    }


}
